<?php

class Person extends AppModel {
	var $name = 'Person';

	var $useTable = 'mnhis_person';
	
	const PERSON_COMPANY = 1;
	
	protected $fields = array(
		'id' => array('name' => 'ID'),
		'name' => array('name' => 'Meno'),
		'surname' => array('name' => 'Priezvisko'),
		'email' => array('name' => 'E-mail'),
		'phone' => array('name' => 'Telefónne číslo'),
		'birth_day' => array('name' => 'Deň narodenia'),
		'birth_month' => array('name' => 'Mesiac narodenia'),
		'birth_year' => array('name' => 'Rok narodenia'),
		'fk_parent_person_id' => array('name' => 'Nadriadený v rámci štruktúry', 'reference' => array('model' => 'Person', 'field' => 'full_name', 'default' => 'Nikto')),
		'fk_person_type_id' => array('name' => 'Typ osoby', 'reference' => array('model' => 'PersonType', 'field' => 'title', 'default' => 'Žiadny')),
	);
	
	var $belongsTo = array(
		'Parent' => array(
			'className' => 'Person',
			'foreignKey' => 'fk_parent_person_id',
		),
		'PersonType' => array(
			'className' => 'PersonType',
			'foreignKey' => 'fk_person_type_id',
		)
	);

	var $hasMany = array(
		// toto sa da pouzit len ked vyberame rekurzivne na uroven 1, inak treba pouzit metodu getChildren()
		'Children' => array(
			'className'     => 'Person',
			'foreignKey'    => 'fk_parent_person_id',
			'conditions'    => array('Children.fk_parent_person_id != ' => '0'),
			'dependent'=> true
		)
	);

	var $validate = array(
        'name' => array(
			'rule' => array('minLength', 1),
            'required' => TRUE,
			'message' => 'Musíte vyplniť meno',
		),
		'surname' => array(
			'rule' => array('minLength', 1),
            'required' => TRUE,
			'message' => 'Musíte vyplniť priezvisko',
		),
		'email' => array(
			'rule' => array('email', true),
			'message' => 'Musíte zadať platnú emailovú adresu',
		),
		'fk_person_type_id' => array(
			'rule' => array('comparison', '>', 0),
			'message' => 'Musíte vybrať typ'
		)
	);

	var $relations = array(
		'User' => array(
			'fk_person_id' => array(
				'field' => 'username',
				'message_one' => 'Používateľ: ###PLACEHOLDER### musí byť najprv zmazaný alebo priradený inej osobe',
				'message_more' => 'Používatelia: ###PLACEHOLDER### musia byť najprv zmazaní alebo priradení inej osobe',
			),
		),
		'Person' => array(
			'fk_parent_person_id' => array(
				'field' => 'full_name',
				'message_one' => 'Osoba: ###PLACEHOLDER### musí byť najprv zmazaná alebo priradená inému nadriadenému',
				'message_more' => 'Osoby: ###PLACEHOLDER### musia byť najprv zmazané alebo priradené inému nadriadenému',
			),
		),
		'Market' => array(
			'fk_client_id' => array(
				'field' => 'description',
				'message_one' => 'Ponuka / dopyt: ###PLACEHOLDER### musí byť najprv zmazaná alebo priradená inému klientovi',
				'message_more' => 'Ponuky / dopyty: ###PLACEHOLDER### musia byť najprv zmazané alebo priradené inému klientovi',
			),
			'fk_tipster_id' => array(
				'field' => 'description',
				'message_one' => 'Ponuka / dopyt: ###PLACEHOLDER### musí byť najprv zmazaná alebo priradená inému tiperovi',
				'message_more' => 'Ponuky / dopyty: ###PLACEHOLDER### musia byť najprv zmazané alebo priradené inému tiperovi',
			),
			'fk_broker_id' => array(
				'field' => 'description',
				'message_one' => 'Ponuka / dopyt: ###PLACEHOLDER### musí byť najprv zmazaná alebo priradená inému maklérovi',
				'message_more' => 'Ponuky / dopyty: ###PLACEHOLDER### musia byť najprv zmazané alebo priradené inému maklérovi',
			),
		),
		'Commission' => array(
			'fk_person_id' => array(
				'field' => 'id',
				'message_one' => 'Depozit: ###PLACEHOLDER### musí byť najprv zmazaný alebo priradený inej osobe',
				'message_more' => 'Depozity: ###PLACEHOLDER### musia byť najprv zmazané alebo priradené inej osobe',
			),
		),
	);
	
	public function __construct($id = false, $table = null, $ds = null) {
		parent::__construct($id, $table, $ds);
		// full name s titulmi
//		$this->virtualFields['full_name'] = trim(sprintf('CONCAT(%s.titles_before, " ", %s.name, " ", %s.surname, " ", %s.titles_after)',
//				$this->alias, $this->alias, $this->alias, $this->alias));

		// full_name bez titulov
		$this->virtualFields['full_name'] = trim(sprintf('CONCAT(%s.name, " ", %s.surname)', $this->alias, $this->alias));

		// virtualny field, ktory urci, ci ma osoba vyplneny datum narodenia - ak je vysledok nasobenia rovny 0, tak nema nieco vyplnene
		$this->virtualFields['birthday_check'] = sprintf('(%s.birth_day * %s.birth_month * %s.birth_year)',
				$this->alias, $this->alias, $this->alias);

		$this->virtualFields['birthday'] = trim(sprintf('CONCAT(%s.birth_day, ". ", %s.birth_month, ". ", %s.birth_year)',
				$this->alias, $this->alias, $this->alias));
	}

	/**
	 * returns all children of actual person
	 *
	 * @return	array
	 */
	public function getChildren() {
		$children = $this->find('all', array('conditions' => array('Person.fk_parent_person_id' => $this->id)));
		return $children;
	}

	public function getBitches() {
		$bitchList = array();
		// TODO rovno tu pocitajme aj % ktore jednotlivi ludia dostanu
		$person = $this->find('first', array('conditions' => array('Person.id' => $this->id)));
		if ($person && ($person['PersonType']['commission_1_tipster'] || $person['PersonType']['commission_2_tipsters'])) {
			$commissionZeroTipsters = 0;
			$commissionOneTipster = 0;
			$commissionTwoTipsters = 0;
			$commissionZeroTipsters += $person['PersonType']['commission_0_tipsters'];
			$commissionOneTipster += $person['PersonType']['commission_1_tipster'];
			$commissionTwoTipsters += $person['PersonType']['commission_2_tipsters'];

			$person['Commission'] = array(
				'commission_0_tipsters' => $commissionZeroTipsters,
				'commission_1_tipster' => $commissionOneTipster,
				'commission_2_tipsters' => $commissionTwoTipsters,
			);
			$bitchList[] = $person;

			$check = 0;
			while (!$person['PersonType']['top_of_group']) {
				if ($check >= 10) {
					throw new Exception('Person has too many parents');
					break;
				}
				$parent = $this->find('first', array('conditions' => array('Person.id' => $person['Person']['fk_parent_person_id'])));
				if ($parent) {
					if ($parent['PersonType']['id'] != $person['PersonType']['id']) {
						$parent['Commission'] = array(
							'commission_0_tipsters' => $parent['PersonType']['commission_0_tipsters'] - $commissionZeroTipsters,
							'commission_1_tipster' => $parent['PersonType']['commission_1_tipster'] - $commissionOneTipster,
							'commission_2_tipsters' => $parent['PersonType']['commission_2_tipsters'] - $commissionTwoTipsters,
						);
						$commissionZeroTipsters += $parent['Commission']['commission_0_tipsters'];
						$commissionOneTipster += $parent['Commission']['commission_1_tipster'];
						$commissionTwoTipsters += $parent['Commission']['commission_2_tipsters'];
						$bitchList[] = $parent;
					}
					$person = $parent;
				} else {
					break;
				}
			}
//			
//			$bitchList = array_merge(array($company), $bitchList);
		}
		// print_r($bitchList);
		return $bitchList;
	}
}

?>